package com.rockxin.cat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.rockxin.cat.MainActivity;
import com.rockxin.cat.R;
import com.rockxin.cat.adapter.SettingsAdapter;
import com.rockxin.cat.base.AppManager;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.base.User;
import com.rockxin.cat.event.LoginOutEvent;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.JumpActivityManager;
import com.rockxin.cat.utils.SPUtils;
import com.rockxin.cat.utils.ToastUtils;

import org.simple.eventbus.EventBus;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;

public class SettingsActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.loginOut)
    TextView loginOut;

    private SettingsActivity mSettingsActivity;
    // private String[] nameList = {"个人资料", "实名认证", "我的银行卡", "修改登录密码", "修改支付密码", "推荐奖励", "版本更新"};
    private String[] nameList = {"个人资料", "实名认证", "我的银行卡", "修改登录密码", "推荐奖励"};
    private CatNetWork catNetWork;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSettingsActivity = this;
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_settings;
    }

    @Override
    public void initData() {
        title.setText("设置");
        recyclerView.setLayoutManager(new LinearLayoutManager(mSettingsActivity));
        SettingsAdapter mSettingsAdapter = new SettingsAdapter(Arrays.asList(nameList));
        mSettingsAdapter.setOnItemChildClickListener(this);
        recyclerView.setAdapter(mSettingsAdapter);

        if (User.getInstance().isLogin()) {
            loginOut.setText("退出登录");
        } else {
            loginOut.setText("去登录");
        }

        if (catNetWork == null) {
            catNetWork = new CatNetWork();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (User.getInstance().isLogin()) {
            loginOut.setText("退出登录");
        } else {
            loginOut.setText("去登录");
        }
    }

    @OnClick({R.id.back_img, R.id.loginOut})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                finish();
                break;
            case R.id.loginOut:
               /* SPUtils spUtils = new SPUtils("Guide");
                String token = spUtils.getString(Constant.PREFERENCES.TOKEN);
                catNetWork.logout(token, new Subscriber<HttpResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult httpResult) {
                        if (httpResult.getStatus() == 1) {
                            SPUtils spUtils = new SPUtils("Guide");
                            spUtils.put(Constant.PREFERENCES.PHONE, "");
                            spUtils.put(Constant.PREFERENCES.PASSWORD, "");
                            spUtils.put(Constant.PREFERENCES.TOKEN, "");
                            AppManager.getInstance().killAll();
                            startActivity(new Intent(SettingsActivity.this, SplashActivity.class));
                        }
                    }
                });*/
                if (User.getInstance().isLogin()) {
                    catNetWork.logout(User.getInstance().getToken(), new Subscriber<HttpResult>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(HttpResult httpResult) {
                            if (httpResult.getStatus() == 1) {
                                SPUtils spUtils = new SPUtils("Guide");
                                spUtils.put(Constant.PREFERENCES.PHONE, "");
                                spUtils.put(Constant.PREFERENCES.PASSWORD, "");
                                spUtils.put(Constant.PREFERENCES.TOKEN, "");
                                User.getInstance().clearUserInfo();
//                                EventBus.getDefault().post(new LoginOutEvent(), "loginout");
//                                loginOut.setText("去登录");
                                AppManager.getInstance().killAll();
                                startActivity(new Intent(SettingsActivity.this, MainActivity.class));
                            }

                        }
                    });
                }
                break;
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (!User.getInstance().isLogin()) {
            JumpActivityManager.getInstance().jumpLoginActivity(mSettingsActivity);
            return;
        }
        switch (position) {
            case 0:
                JumpActivityManager.getInstance().jumpPersonalData(mSettingsActivity);
                break;
            case 1:
                JumpActivityManager.getInstance().jumpRealNameActivity(mSettingsActivity);
                break;
            case 2:
                JumpActivityManager.getInstance().jumpMyBankCardActivity(mSettingsActivity);
                break;
            case 3:
                startActivity(new Intent(SettingsActivity.this, ModifyLoginPasswdActivity.class));
                break;
            case 4:
                JumpActivityManager.getInstance().jumpRecommendAwardActivity(mSettingsActivity);
                break;
//            case 4:
//                if (true) {
//                    //设置支付密码
//                    startActivity(new Intent(SettingsActivity.this, SetupPaymentPasswdActivity.class));
//                } else {
//                    //修改支付密码
//                    startActivity(new Intent(SettingsActivity.this, ModifyPaymentPasswdActivity.class));
//                }
//
//                break;
//            case 5:
//                JumpActivityManager.getInstance().jumpRecommendAwardActivity(mSettingsActivity);
//                break;
        }
    }
}
